package com.webside.play.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.filter.config.ConfigTools;
import com.webside.cdn.service.AuthBiz;
import com.webside.enums.ValidateMsg;
import com.webside.play.service.PlayService;
import com.webside.site.service.SiteService;
import com.webside.util.JsonResult;
@Controller
@RequestMapping("/play")
public class PlayController {
	
	@Autowired
	SiteService siteService;
	@Autowired
	PlayService playService;
	@Autowired
	private AuthBiz authBiz;
	
	public static String cdnBasePath="http://12.45.21.34:8080";
	@RequestMapping(value="/validate")
	@ResponseBody
	public JsonResult getVideo(HttpServletRequest request, HttpServletResponse response) throws IOException{
		/*String videoId, String appid, Long timestamp, String sign */
		String appid =  request.getParameter("appid");
		Long timestamp;
		try {
			timestamp = Long.parseLong(request.getParameter("timestamp"));
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return JsonResult.success("时间戳格式错误");
		}
		String sign =  request.getParameter ("sign");
		//校验
		String message = playService.validate(appid,timestamp,sign);
		//没有成功通过
		if (!message.equals(ValidateMsg.ACCESS.getMsg())) {
			return JsonResult.success(message);
		}
		//通过校验
		String rid = request.getParameter("rid");//获取视频id
		String hl = request.getParameter("hl");//获取视频清晰度
		String token=null;
		//剧集id非空校验
		if(rid != null ){
			token = authBiz.generateCDNToken();
		}else{
			token = request.getParameter("token");
		}
		System.out.println("经过此节点");
		response.sendRedirect(cdnBasePath+"/vc_media/file/media/"+rid+"/"+hl+"?appid=GuoXin-http&token="+token);
		
		return JsonResult.success();
		
	}
	
	public static void main(String[] args) throws Exception {
		ConfigTools.main(new String[]{"vc@2017"});
	}
	
}
